package com.controller;
import com.pojo.StateInfo;
import com.auto.service.AutoCode;
import com.Config;
import com.common.Global;
import com.common.MyProperties;
import com.common.PropertiesUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * 基础控制层，用于过滤一些通用的连接
 * Created by CFQ
 */
@Controller
public class BaseController {
	@Autowired
	private Config config;
	@Resource(name = "autoCode")
	private AutoCode autoCode;
	/**
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/")
	public String index(HttpServletRequest request, Model model) throws FileNotFoundException {
		String msg = null;
		try {
			File file = ResourceUtils.getFile("config/ATC_Config.properties");
			if (!file.exists()) {
				/**
				 * 发现配置文件，如果配置文件不存在则跳转配置界面
				 */
//				return "forward:config";
				return "redirect:config";
			} else {
				/**
				 * 配置界面存在则进入自动构建并输出信息。
				 */
				MyProperties prop = PropertiesUtil.readOrderedPropertiesFile(file);
				config = new Config(prop);
				this.changeConfig2Global(config);
				return "main";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = e.getMessage();
			Logger.getLogger(this.getClass()).error(e.getMessage());
		}
		model.addAttribute("msg", msg);
		return "500";
	}

	/**
	 * 获取配置界面
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/config",method = RequestMethod.GET)
	public String getConfig(HttpServletRequest request, Model model) {
		File file = null;
		try {
			file = ResourceUtils.getFile("config/ATC_Config.properties");
			if (file.exists()) {
				MyProperties prop = PropertiesUtil.readOrderedPropertiesFile(file);
				config = new Config(prop);
			}
			model.addAttribute("config",config);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			model.addAttribute("msg", e.getMessage());
			return "500";
		}
		return "config";
	}

	/**
	 * 保存参数
	 * @param request
	 * @param model
	 * @param config
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/config",method = RequestMethod.POST)
	public StateInfo saveConfig(HttpServletRequest request, Model model,@ModelAttribute("config") Config config) {
		StateInfo stateInfo = new StateInfo();
		File file = null;
		try {
			/**
			 * 创建文件
			 */
			file = ResourceUtils.getFile("config/ATC_Config.properties");
			if (!file.exists()) {
				File fileParent = file.getParentFile();
				if (!fileParent.exists()) {
					fileParent.mkdirs();
				}
				file.createNewFile();
			}
			/**
			 * 保存参数
			 */
			MyProperties prop = new MyProperties(config);
			PropertiesUtil.writeOrderedPropertiesFile(prop, file, "Global Config");

			/**
			 * 保存到全局变量
			 */
			this.changeConfig2Global(config);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			stateInfo.setFlag(false);
			stateInfo.setMsg(e.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
			stateInfo.setFlag(false);
			stateInfo.setMsg(e.getMessage());
		}
		return stateInfo;
	}


	/**
	 * 开始自动编码
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/startCode",method = RequestMethod.GET)
	public StateInfo startCode(HttpServletRequest request, Model model) {
		String flag = request.getParameter("flag");
		StateInfo stateInfo = null;
		if("1".equals(flag)) {
			Global.Send("后台开始撤销生成的文件...");
			stateInfo = autoCode.run(false);
		}else {
			Global.Send("后台开始自动生成文件代码...");
			stateInfo = autoCode.run(true);
		}
		return stateInfo;
	}

	/**
	 * 把Config的值赋值到Global里面
	 * @param config
	 */
	private void changeConfig2Global(Config config) {
		Global.WORKSPACEPROPATH = config.getWORKSPACEPROPATH();
		Global.SRCPATH = config.getSRCPATH();
		Global.CONTROLLER_PACKAGE = config.getCONTROLLER_PACKAGE();
		Global.SERVICE_PACKAGE = config.getSERVICE_PACKAGE();
		Global.SERVICEIMPL_PACKAGE = config.getSERVICEIMPL_PACKAGE();
		Global.DAO_PACKAGE = config.getDAO_PACKAGE();
		Global.MAPPING_PACKAGE = config.getMAPPING_PACKAGE();
		Global.WEBSRCPATH = config.getWEBSRCPATH();
		Global.JSSRCPATH = config.getJSSRCPATH();
		Global.JSP_PACKAGE = config.getJSP_PACKAGE();
	}

}

